﻿using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shaco
{
    public class SimulatePlatformEventUGUI : shaco.Base.ISimulatePlatformEvent
    {
        public UnityEngine.UI.GraphicRaycaster raycaster;

        private List<RaycastResult> _raycastResult = new List<RaycastResult>();
        private Vector2 _cachedPoint = Vector2.zero;

        /// <summary>
        /// 点击对象
        /// <param name="name">对象名字</param>
        /// <return>是否点击中对象</return>
        /// </summary>
        public bool PressGameObject(string name)
        {
            var gameObjectTarget = GameObject.Find(name);
            if (null != gameObjectTarget)
            {
                gameObjectTarget = ExecuteEvents.ExecuteHierarchy(gameObjectTarget, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
            }
            return null != gameObjectTarget;
        }

        /// <summary>
        /// 点击位置
        /// PS：该方法默认仅对UGUI对象生效，如果需要对非UGUI对象生效，则需要按照以下步骤操作
        ///  1：给对象添加IPointerClickHandler接口并实现接口方法
        ///  2：给摄像机添加PhysicsRaycaster组件以响应事件
        /// <param name="点击横坐标">x</param>
        /// <param name="点击纵坐标">y</param>
        /// </summary>
        public object ClickPoint(float x, float y, shaco.Base.SimulatePlatformEventType type = shaco.Base.SimulatePlatformEventType.PointClick)
        {
            if (null == EventSystem.current)
            {
                shaco.Log.Warning("SimulatePlatformEventUGUI ClickPoint warning: EventSystem.current is null");
                return null;
            }

            var eventdata = new PointerEventData(EventSystem.current);
            _cachedPoint.x = x;
            _cachedPoint.y = y;
            eventdata.position = _cachedPoint;

            EventSystem.current.RaycastAll(eventdata, _raycastResult);
            if (_raycastResult.Count > 0)
            {
                var target = _raycastResult[0].gameObject;
                switch (type)
                {
                    case shaco.Base.SimulatePlatformEventType.PointMove: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerMoveHandler);
                    case shaco.Base.SimulatePlatformEventType.PointEnter: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerEnterHandler);
                    case shaco.Base.SimulatePlatformEventType.PointExit: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerExitHandler);
                    case shaco.Base.SimulatePlatformEventType.PointDown: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerDownHandler);
                    case shaco.Base.SimulatePlatformEventType.PointUp: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerUpHandler);
                    case shaco.Base.SimulatePlatformEventType.PointClick: return (object)ExecuteEvents.ExecuteHierarchy(target, eventdata, ExecuteEvents.pointerClickHandler);
                    default: Log.Error("SimulatePlatformEventUGUI ClickPoint error: unsupport type=" + type); return null;
                }
            }
            else
                return null;
        }

        // /// <summary> 目前暂时无该功能需求，暂时不做了
        // /// 触摸滚动屏幕
        // /// <param name="name">对象名字</param>
        // /// <param name="startX">触摸开始x坐标</param>
        // /// <param name="startY">触摸开始y坐标</param>
        // /// <param name="endX">触摸结束x坐标</param>
        // /// <param name="endY">触摸结束y坐标</param>
        // /// </summary>
        // public bool DragPosition(string name, float startX, float startY, float endX, float endY)
        // {
        //     var gameObjectTarget = GameObject.Find(name);
        //     if (null != gameObjectTarget)
        //     {
        //         var startPosition = new Vector2(startX, startY);
        //         var endPosition = new Vector2(endX, endY);
        //         gameObjectTarget = ExecuteEvents.ExecuteHierarchy(gameObjectTarget, new PointerEventData(EventSystem.current)
        //         {
        //             position = startPosition
        //         }, ExecuteEvents.beginDragHandler);
        //         gameObjectTarget = ExecuteEvents.ExecuteHierarchy(gameObjectTarget, new PointerEventData(EventSystem.current)
        //         {
        //             position = endPosition
        //         }, ExecuteEvents.dragHandler);
        //         gameObjectTarget = ExecuteEvents.ExecuteHierarchy(gameObjectTarget, new PointerEventData(EventSystem.current)
        //         {
        //             position = endPosition
        //         }, ExecuteEvents.endDragHandler);
        //     }
        //     return null != gameObjectTarget;
        // }
    }
}